<?php

require_once 'common/conn.php';
$data = array();
if (isset($_GET['usercode'])) {
    $usercode = $_GET['usercode'];
}
if (isset($_GET['method'])) {
    $method = $_GET['method'];
    $db = new DBC();
    if ($method == "get") {
        $sql = "SELECT usercode,SUM(amount) AS amount FROM t_gold_record WHERE amount > 0 GROUP BY usercode ORDER BY amount DESC";
    } else if ($method == "use") {
        $sql = "SELECT usercode,SUM(amount) AS amount FROM t_gold_record WHERE amount < 0 GROUP BY usercode ORDER BY amount";
    }
    $temp = $db->fetchData($sql);
    if ($temp != false && count($temp) > 0) {
        $count = count($temp);
        for ($i = 0; $i < 6; $i++) {
            $temp[$i]['no'] = $i + 1;
            if ($i < $count) {
                $data[$i] = $temp[$i];
            }
        }
        for ($i = 0; $i < $count; $i++) {
            if ($temp[$i]['usercode'] == $usercode) {
                if ($count < 6) {
                    for ($j = 0; $j < 5; $j++) {
                        $temp[$j]['no'] = $j + 1;
                        if ($j < $count) {
                            $data[$j] = $temp[$j];
                        }
                    }
                } else if ($i < 6) {
                    for ($j = 0; $j < 5; $j++) {
                        $temp[$j]['no'] = $j + 1;
                        if ($j < $count) {
                            $data[$j] = $temp[$j];
                        }
                    }
                } else if ($i == ($count - 1)) {
                    $temp[0]['no'] = 1;
                    $temp[1]['no'] = 2;
                    $temp[2]['no'] = 3;
                    $temp[3]['no'] = $count - 2;
                    $temp[4]['no'] = $count - 1;
                    $temp[5]['no'] = $count;

                    $data[0] = $temp[0];
                    $data[1] = $temp[1];
                    $data[2] = $temp[2];
                    $data[3] = $temp[$i - 2];
                    $data[4] = $temp[$i - 1];
                    $data[5] = $temp[$i];
                } else {
                    $temp[0]['no'] = 1;
                    $temp[1]['no'] = 2;
                    $temp[2]['no'] = 3;
                    $temp[3]['no'] = $i;
                    $temp[4]['no'] = $i + 1;
                    $temp[5]['no'] = $i + 2;

                    $data[0] = $temp[0];
                    $data[1] = $temp[1];
                    $data[2] = $temp[2];
                    $data[3] = $temp[$i - 1];
                    $data[4] = $temp[$i];
                    $data[5] = $temp[$i + 1];
                }
                break;
            }
        }
    }
    $user_array = array();
    if ($data != false && count($data) > 0) {
        for ($i = 0; $i < $count; $i++) {
            if (isset($data[$i]['usercode'])) {
                $user_array[$i] = "'" . $data[$i]['usercode'] . "'";
                $data[$i]['nickname'] = "";
                $data[$i]['sex'] = "-1";
            }
        }
        $str = implode(",", $user_array);
        $sql = "SELECT nickname,devicecode AS usercode,sex FROM t_device WHERE devicecode IN (" . $str . ")";
        $users = $db->fetchData($sql);
        for ($i = 0; $i < count($data); $i++) {
            if (isset($data[$i]['usercode'])) {
                for ($j = 0; $j < count($users); $j++) {
                    if ($users[$j]['usercode'] == $data[$i]['usercode']) {
                        if ($users[$j]['nickname'] == false || $users[$j]['nickname'] == null || $users[$j]['nickname'] == "null" || $users[$j]['nickname'] == "") {
                            $data[$i]['nickname'] = "";
                        } else {
                            $data[$i]['nickname'] = $users[$j]['nickname'];
                        }
                        if ($users[$j]['sex'] == false || $users[$j]['sex'] == null || $users[$j]['sex'] == "null" || $users[$j]['sex'] == "") {
                            $data[$i]['sex'] = '-1';
                        } else {
                            $data[$i]['sex'] = $users[$j]['sex'];
                        }
                    }
                }
            } else {
                unset($data[$i]);
            }
        }
    }
}
echo json_encode($data);
?>